<template>
  <div class="index">
    <div class="video">
      <van-swipe :show-indicators="show_indicators" vertical style="height: 400px; margin-top: 180px" @change="onChange">
        <van-swipe-item v-for="(result, index) in results" :key="index">
          <video :src="result.data.playUrl" controls="controls" style="width: 100%" loop="loop" controlsList="nodownload" :disablePictureInPicture="true" :id="'video' + index"></video>
        </van-swipe-item>
      </van-swipe>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
import { Swipe, SwipeItem } from 'vant'
import { Toast } from 'vant'
export default {
  name: 'Video',
  data() {
    return {
      // scr1: require('@/assets/video.png'),
      show_indicators: false,
      results: [],
      olde_index: 0
    }
  },
  methods: {
    onChange(index) {
      //下滑
      //当前视频
      let video = document.getElementById('video' + index)
      //上一个视频
      let vido2 = document.getElementById('video' + this.olde_index)
      vido2.pause()
      video.play()
      this.olde_index = index
      // console.log(this.results)
    }
  },
  // 单页背景色
  beforeRouteEnter(to, from, next) {
    document.querySelector('body').setAttribute('style', 'background-color:black')
    next()
  },
  beforeRouteLeave(to, from, next) {
    document.querySelector('body').setAttribute('style', '')
    next()
  },
  mounted() {
    var randomNum = Math.random()
    var checkCode = randomNum * 9000
    checkCode = parseInt(checkCode + 1000)
    axios({
      method: 'post',
      url: 'https://api.apiopen.top/videoRecommend?id=' + checkCode,
      data: {}
    }).then((res) => {
      // console.log(res)
      // console.log(checkCode)
      if (res.data.code == 200) {
        this.results = res.data.result
      }
      if (res.data.code == 400) {
        Toast('服务器开小差了,请重新刷新')
      }
    })
  },
  computed: {}
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.index {
  width: 100%;
  /* height: 60%; */
}
/* .video {
  width: 100%;
  height: 400px;
  margin-top: 35%;
} */
/* 隐藏全屏按钮 */
video::-webkit-media-controls-fullscreen-button {
  display: none;
}
video::-webkit-media-controls-mute-button {
  display: none !important;
}
video::-webkit-media-controls-current-time-display {
  display: none;
}
video::-webkit-media-controls-time-remaining-display {
  display: none;
}
/* video::-webkit-media-controls-play-button {
  position: absolute;
  left: 45%;
  top: 10%;
} */
</style>
